//
//  HomeViewController.swift
//  swiftDemo
//
//  Created by Gerry on 2021/10/27.
//

import UIKit


private let kTitleViewH : CGFloat = 40

class HomeViewController: UIViewController {

    private lazy var pageTitleView : PageTitleView = { [weak self] in
        let titleFrame = CGRect.init(x: 0, y: kStatusBar+kNavigationBar, width: UIScreen.main.bounds.size.width, height: kTitleViewH)
        let titles = ["推荐", "游戏", "娱乐", "趣玩"]
        let titleView = PageTitleView(frame: titleFrame, titles: titles)
//        titleView.backgroundColor = .cyan
        titleView.delegate = self
        return titleView
    }()
    
    
    private lazy var contentView : PageContentView = { [weak self] in
        let y = kStatusBar + kNavigationBar + kTitleViewH
        let h = kScreenH - y - kTabbar
        let contentFrame = CGRect.init(x: 0, y: y, width: kScreenW, height: h)
        
        var childVCs = [UIViewController]()
        let recommendVC = RecommendViewController()
        recommendVC.view.frame = CGRect.init(x: 0, y: 0, width: contentFrame.width, height: contentFrame.height)
        childVCs.append(recommendVC)
        
        let gameVC = GameViewController()
        gameVC.view.frame = CGRect.init(x: 0, y: 0, width: contentFrame.width, height: contentFrame.height)
        childVCs.append(gameVC)
        
        let amuseVC = AmuseViewController()
        amuseVC.view.frame = CGRect.init(x: 0, y: 0, width: contentFrame.width, height: contentFrame.height)
        childVCs.append(amuseVC)
        
        for _ in 0..<1 {
            let vc = UIViewController()
            vc.view.backgroundColor = UIColor.init(r: CGFloat(arc4random_uniform(255)), g: CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255)))
            
            childVCs.append(vc)
        }
        
        let contentView = PageContentView(frame: contentFrame, childVCs: childVCs, parentVC: self)
        contentView.delegate = self
        return contentView
    }()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 不需要调整scollview的内边距
        // automaticallyAdjustsScrollViewInsets

        self.title = "首页"
        
        // Do any additional setup after loading the view.
        let label = UILabel.init(frame: CGRect.init(x: 60, y: 260, width: 100, height: 30))
        label.text = "Home"
        self.view .addSubview(label)
        
        setupNavigationBar()
        setupUI()
    }
    
    func setupUI() {
        view.addSubview(pageTitleView)
        view.addSubview(contentView)
    }

}

extension HomeViewController {
    func setupNavigationBar() {
    }
}

extension HomeViewController : PageTitleViewDelegate {
    func pageTitleView(pageTitleView: PageTitleView, selectedIndex index: Int) {
        print(index)
        
        contentView.setCurrentPageIndex(index: index)
    }
}

extension HomeViewController : PageContentViewDelegate {
    func contentOffsetChanged(pageContentView: PageContentView, progress: CGFloat, scrollDirection: ScrollDirection) {
        print("progress:\(progress), scrollDirection:\(scrollDirection)")
        
        // 通知title tab切换
        pageTitleView.setCurrentTabIndex(progress: progress, scrollDirection: scrollDirection)
    }
}



